package com.gdchent.rn;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.gdchent.rn.bean.ProductInfo;
import com.gdchent.rn.dao.DbHelper;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

import java.util.List;

public class MainActivity extends ReactActivity {

  protected int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 0;
  protected final String TAG = "gdchent";

  /**
   * Returns the name of the main component registered from JavaScript.
   * This is used to schedule rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "RNOneKeyBuyCoins";
  }

  @Override
  protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {
      @Override
      protected ReactRootView createRootView() {
        return new RNGestureHandlerEnabledRootView(MainActivity.this);
      }
    };
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("gdchentbbbbb","onCreate:");
    new Thread(new Runnable() {
      @Override
      public void run() {
        DbHelper.getInstance(MainActivity.this);
      }
    }).start();
    //大于6.0动态配置RN 权限
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      requestPermission();
    }
  }

  private void requestPermission() {

    Log.i(TAG, "requestPermission");
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
      Manifest.permission.CALL_PHONE)
      != PackageManager.PERMISSION_GRANTED) {
      Log.i(TAG, "checkSelfPermission");
      // Should we show an explanation?
      if (ActivityCompat.shouldShowRequestPermissionRationale(this,
        Manifest.permission.CALL_PHONE)
        && ActivityCompat.shouldShowRequestPermissionRationale(this,
        Manifest.permission.READ_EXTERNAL_STORAGE) && ActivityCompat.shouldShowRequestPermissionRationale(this,
        Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        Log.i(TAG, "shouldShowRequestPermissionRationale");
        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

        ActivityCompat.requestPermissions(this,
          new String[]{
            Manifest.permission.CALL_PHONE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET
          },
          MY_PERMISSIONS_REQUEST_READ_CONTACTS);

      } else {
        Log.i(TAG, "requestPermissions");
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(this,
          new String[]{
            Manifest.permission.CALL_PHONE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET
          },
          MY_PERMISSIONS_REQUEST_READ_CONTACTS);
        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
      }
    }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode,
                                         String permissions[], int[] grantResults) {
    if(requestCode==MY_PERMISSIONS_REQUEST_READ_CONTACTS){
      if (grantResults.length > 0
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "onRequestPermissionsResult granted");
        // permission was granted, yay! Do the
        // contacts-related task you need to do.

      } else {
        Log.i(TAG, "onRequestPermissionsResult denied");
        // permission denied, boo! Disable the
        // functionality that depends on this permission.
        showWaringDialog();
      }
      return;
    }
  }
  //开启警告权限
  private void showWaringDialog() {
    AlertDialog dialog = new AlertDialog.Builder(this)
      .setTitle("警告！")
      .setMessage("请前往设置->应用->PermissionDemo->权限中打开相关权限，否则功能无法正常运行！")
      .setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          // 一般情况下如果用户不授权的话，功能是无法运行的，做退出处理
          finish();
        }
      }).show();
  }

}
